package com.zhht.checkcop.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhbc.framework.support.Result;
import com.zhbc.framework.support.Transform;
import com.zhbc.framework.utils.StringUtils;
import com.zhht.checkcop.check.vo.UserVO;
import com.zhht.checkcop.service.UserService;

@Controller
public class UserCotroller {

	@Autowired
	private UserService userService;
	
	private final static Logger log = Logger.getLogger(UserCotroller.class);;

	@RequestMapping(value = "/user/login", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String login(@RequestParam String param, HttpServletRequest request, HttpServletResponse re) {
		Result r = new Result();
		Transform<Map<String, Object>> p = Transform.fromJson(param, Map.class);
		Map<String, Object> data = p.getData();
		try {
			String userName = data.get("userName").toString();
			String passWord = data.get("passWord").toString();
			UserVO user = userService.getUserByName(userName);
			if (user == null) {
				return r.error("用户不存在/无效!");
			}
//			if (passWord == null || !(user.getPass_word().equals(passWord))) {
//				return r.error("用户名或者密码错误!");
//			}
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("user", user);
			r.setData(map);
		} catch (Exception e) {
			e.printStackTrace();
			return r.failure("请求失败");
		}
		return r.success();
	}
	
	@RequestMapping(value = "/user/add", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String add(@RequestParam String param, HttpServletRequest request, HttpServletResponse re) {
		Result r = new Result();
		Transform<Map<String, Object>> p = Transform.fromJson(param, Map.class);
		Map<String, Object> data = p.getData();
		try {
			String userName = data.get("userName")+"";
			if(StringUtils.isStrTrimNull(userName)){
				return r.error("手机号码为空");
			}
			
			String passWord = data.get("passWord").toString();
			if(StringUtils.isStrTrimNull(passWord)){
				return r.error("密码不能为空");
			}
			String userUUid = data.get("userUUid").toString();
			String name = data.get("name").toString();
			UserVO user = userService.getUserByName(userName);
			if (user == null) {
				return r.error("用户不存在/无效!");
			}
			if (passWord == null || !(user.getPass_word().equals(passWord))) {
				return r.error("用户名或者密码错误!");
			}
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("user", user);
			r.setData(map);
		} catch (Exception e) {
			e.printStackTrace();
			return r.failure("请求失败");
		}
		return r.success();
	}
}
